import pygame, sys
from pygame.locals import *
import random

win_w = 800
win_h = 600
cell_w, cell_h = 25, 25
IMAGE_PATH = 'imgs/snake'


class Main():
    li = [(50, 400), (25, 400)]
    direction = 0
    pygame.init()
    source_img = pygame.image.load(IMAGE_PATH + "/head.png")
    source_apple_img = pygame.image.load(IMAGE_PATH + "/apple.png")
    headimg = pygame.transform.scale(source_img, (25, 25))
    apple_img = pygame.transform.scale(source_apple_img, (25, 25))
    DISPLAYSUF = pygame.display.set_mode((win_w, win_h))
    pygame.display.set_caption("贪吃蛇")
    clock = pygame.time.Clock()
    eat = True
    finish = False
    food = (0, 0)

    def draw_snake(self, li, direction):
        print("-------%s" % direction)
        index = 0
        for (x, y) in li:
            if index == 0:
                self.DISPLAYSUF.blit(self.headimg, (x, y))
            else:
                pygame.draw.rect(self.DISPLAYSUF, (0, 225, 255), (x, y, cell_w, cell_h))
            index = index + 1
        (hx, hy) = li[0]
        if direction == 0:
            hx = hx + cell_w
            if hx > 800:
                hx = 0
        if direction == 1:
            hx = hx - cell_w
            if hx < 0:
                hx = 800
        if direction == 2:
            hy = hy + cell_h
            if hy > 600:
                hy = 0
        if direction == 3:
            hy = hy - cell_h
            if hy < 0:
                hy = 600
        head = (hx, hy)
        if self.in_body(head):
            # print("==============")
            # print(head)
            # print(self.li)
            self.finish = True

        li.insert(0, head)
        print("-----%s,%s", hx, hy)
        if head == self.food:
            self.eat = True
        else:
            li.pop()

    def in_body(self, cel):
        insnake = False
        for i in self.li:
            if i == cel:
                insnake = True
                break
        return insnake

    def draw_food(self):
        while self.eat:
            x = random.randint(1, int(win_w / 25) - 1) * cell_w
            y = random.randint(1, int(win_h / 25) - 1) * cell_h
            new_cell = (x, y)
            insnake = self.in_body(new_cell)
            if not insnake:
                self.food = new_cell
                self.eat = False
                break
        print(self.food)
        (fx, fy) = self.food
        if fx > 0:
            self.DISPLAYSUF.blit(self.apple_img, (fx, fy))
            # pygame.draw.rect(self.DISPLAYSUF, (255, 255, 255), (fx, fy, cell_w, cell_h))

    def start(self):
        while True:
            if self.finish:
                break
            for event in pygame.event.get():
                if event.type == QUIT:
                    pygame.quit()
                    sys.exit()
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_LEFT:
                        if self.direction > 1:
                            self.direction = 1
                    if event.key == pygame.K_RIGHT:
                        if self.direction > 1:
                            self.direction = 0
                    if event.key == pygame.K_UP:
                        if self.direction < 2:
                            self.direction = 3
                    if event.key == pygame.K_DOWN:
                        if self.direction < 2:
                            self.direction = 2

            self.DISPLAYSUF.fill((0, 0, 0))
            self.draw_snake(self.li, self.direction)
            self.draw_food()
            pygame.display.update()
            self.clock.tick(5)


m = Main()
m.start()
